﻿$(document).ready(function () {
    var url = new Url();
    var contractTypeByRole = new ContractTypeByRole();
    var $css = url.GetQueryString("css");
    //分页设置
    var page = new Page();
    page.Dom = $("#searchArea");
    page.CallBack = function (data) {
        $(".contractInfo").empty();
        var isCredit = $("#displayMode").attr("data-credit") === "True";
        var isSettlement = $("#displayMode").attr("data-settlement") === "True";
        var loan = $("#contractType").val() !== "理财流程";
        $.each(data, function (index, item) {
            item.Css = $css;
            item.IsCredit = isCredit;
            item.IsSettlement = isSettlement;
            item.Verifiable = item.Verifiable ? "inline" : "none";
            item.Editable = item.Editable ? "inline" : "none";
            item.CreateContract = item.CreateContract ? "inline" : "none";
            item.Reconsider = item.Reconsider ? "inline" : "none";
            item.Printable = item.Printable ? "inline" : "none";
            item.ViewPage = loan ? (isCredit ? "LoanCreditPreview" : "LoanOrderPreview") : "FinancialOrderDetail";
            item.EditPage = loan ? "LoanNewProj" : "FinancialNewProj";
            item.VerifyPage = loan ? "LoanReview" : "ContractReview";
            item.ContractReview = item.ContractReview ? "inline" : "none";
            item.Allow = item.Allow ? "inline" : "none";
            item.Deductable = item.Deductable ? "inline" : "none";
            item.RefuseSignable = item.RefuseSignable ? "inline" : "none";
            item.LaonPayable = item.LaonPayable ? "inline" : "none";
            item.IsLoan = loan;
            $("#ContractTemplete").tmpl(item).appendTo($(".contractInfo"));
        });

        $(".btnRedemption").bind("click", function () {
            contractTypeByRole.GetOrderDetails($(this).attr("data-id"));
        });
    };
    //权限判断
    if ($("#contractType option").length > 0) {
        //业务类型下拉列表触发
        $("#contractType").change(function () {
            var typeName = $("#contractType").val();
            if (typeName == "理财流程") {
                page.Url = "../Handler/FinancialOrder.ashx";
                page.type = 6;
            }
            else {
                page.Url = "../Handler/LoanProductOrderHandler.ashx";
                page.type = 13;
            }
            var typeName = $("#contractType").val();
            var statusList = contractTypeByRole.GetContractType(typeName);
            $("#verifyStatus").html(statusList);
        }).change();
        $("#btnSearch").click(function () { page.GetPageData(); }).click();
    }
    else {
        $("#caption").text("暂无数据！");
    }
    //搜索用户
    $(".btnSearchBorrow").bind("click", function () {
        var $userName = $(".txtName").val();
        var $phone = $(".txtPhone").val();
        $.getJSON("../Handler/FinancialUser.ashx",
            { UserName: $userName, Phone: $phone, type: 10, _: Math.random() },
            function (result) {
                if (result.Status) {
                    //清空用户列表
                    $(".userInfo").empty();
                    //绑定模板
                    if (result.Count != 0) {
                        $(result.Data).each(function (index, item) {
                            $("#UserTemplete").tmpl(item).appendTo($(".userInfo"));
                        });
                    }
                    else {
                        $(".userInfo").empty();
                        alert("未找到相关数据！");
                    }
                    //绑定单击行事件
                    $(".userInfo tr").css("cursor", "pointer").click(function () {
                        $(".txtuserName").val($(this).find(".name").text());
                        $(".txtTel").val($(this).find(".phone").text());
                        $(".txtUserId").val($(this).find(".userId").text());
                    });
                }
            });
    });

    $(".btnDelLender").bind("click", function () {
        $(".txtuserName").val("");
        $(".txtUserId").val("");
    });
    //选择用户
    $(".btnShow").bind("click", function () {
        $(".txtName").val($(".txtUserName").val());
    })
    //单击用户名文本框时选择用户
    $(".txtuserName").bind("click", function () {
        $(".btnShow").click();
    });
});
$(".btnSendRedemption").bind("click", function () {
    var OrderId = $("input[data-name='OrderId']").val();
    alert(OrderId);
    $.ajax({
        type: "post",
        url: "../Handler/CreditMatchHandler.ashx",
        dataType: "json",
        data: { "type": 6, "OrderId": OrderId, _: Math.random() },
        cache: false,
        success: function (result) {
            if (result.Status) {
                if (result.Data == "0000") {
                    alert(result.Message);
                }
                window.location.reload();
            }
            else {
                alert(result.Message);
            }
        }
    });
});
var ContractTypeByRole = function () { };
ContractTypeByRole.prototype = {
    GetContractType: function (typeName) {
        var result = "";
        $.ajax({
            type: "post",
            url: "../Handler/VerifyStatusHandler.ashx",
            dataType: "json",
            data: { type: 2, FolwName: typeName },
            async: false,
            cache: true,
            complete: function () {
            },
            success: function (data) {
                if (data.Status) {
                    var str = '<option value="">请选择</option>';
                    $(data.Data).each(function (i, e) {
                        str += '<option value="' + e.VerifyId + '">' + e.VerifyName + '</option>';
                    });
                    result = str;
                }
            }
        });
        return result;
    },
    GetOrderDetails: function (id) {
        $("label[data-name='Amount']").text("");
        $("label[data-name='TransferBackBankName']").text("");//银行
        $("label[data-name='TransOpenBank']").text("");//开户行
        $("label[data-name='TransAccName']").text("");//账户名
        $("label[data-name='TransAccNum']").text("");//账户号码
        $("label[data-name='Term']").text("");
        $("input[data-name='OrderId']").val(id);
        $.ajax({
            type: "post",
            url: "../Handler/CreditMatchHandler.ashx",
            dataType: "json",
            data: { type: 5, OrderId: id, _: Math.random() },
            cache: true,
            success: function (result) {
                if (result.Status) {
                    $("label[data-name='Amount']").text(result.Data.Amount);
                    $("label[data-name='TransferBackBankName']").text(result.Data.DeductBankName);//划扣银行编码
                    $("label[data-name='TransOpenBank']").text(result.Data.DeductBranchBank);//划扣开户行
                    $("label[data-name='TransAccName']").text(result.Data.UserName);//收款账户名
                    $("label[data-name='TransAccNum']").text(result.Data.DeductBankAccount);//账户号码

                    if (result.Data.Order.ProjFeeStatTag != 490) {
                        $(".btnSendRedemption").css("display", "none");
                    }
                    else {
                        $(".btnSendRedemption").css("display", "block");
                    }
                    if (result.Data.Order.ProjFeeStatTag != 490) {
                        $(".btnSendRedemption").css("display", "none");
                    }
                    else {
                        $(".btnSendRedemption").css("display", "block");
                    }
                }
                else {
                    alert(result.Message);
                }
            }
        });
    }
};